import type { FormSchemaGetter } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';
import { userList } from '#/api/system/user';
import { customerList } from '#/api/trade/customer';

import { getDictOptions } from '#/utils/dict';
import { renderDict } from '#/utils/render';

export const querySchema: FormSchemaGetter = () => [

  {
    component: 'Input',
    fieldName: 'contractNo',
    label: '合同号',
  },
  {
    component: 'DatePicker',
    componentProps: {
      showTime: true,
      format: 'YYYY-MM-DD HH:mm:ss',
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
    },
    fieldName: 'contractDate',
    label: '合同日期',
  },
  {
    component: 'ApiSelect',
    componentProps: {
      api: () => customerList(),
      resultField: 'rows',
      labelField: 'name',
      valueField: 'id',
      showSearch: true,
      filterOption: (input: string, option: any) => {
        return option.label.toLowerCase().includes(input.toLowerCase());
      },
    },
    fieldName: 'customerId',
    label: '客户名称',
  },
  {
    component: 'ApiSelect',
    componentProps: {
      api: () => userList(),
      resultField: 'rows',
      labelField: 'nickName',
      valueField: 'userId',
      showSearch: true,
      filterOption: (input: string, option: any) => {
        return option.label.toLowerCase().includes(input.toLowerCase());
      },
    },
    fieldName: 'saleUserid',
    label: '销售',
  },
  {
    component: 'Select',
    componentProps: {
      // 可选从DictEnum中获取 DictEnum.CURRENCY 便于维护
      options: getDictOptions('currency'),
    },
    fieldName: 'currency',
    label: '币种',
  },
  {
    component: 'Select',
    componentProps: {
    },
    fieldName: 'approverId',
    label: '审核人',
  },
];

// 基础列配置
const baseColumns: VxeGridProps['columns'] = [
  { type: 'checkbox', width: 60 },
  {
    title: '合同号',
    field: 'contractNo',
    slots: {
      default: 'contractNo'
    },
  },
  {
    title: '合同日期',
    field: 'contractDate',
    formatter: ({ cellValue }) => {
      if (!cellValue) return '';
      // 只显示年月日，不显示时分秒
      return cellValue.split(' ')[0];
    },
  },
  {
    title: '客户名称',
    field: 'customerName',
  },
  {
    title: '销售',
    field: 'saleUserName',
  },
  {
    title: '币种',
    field: 'currency',
    slots: {
      default: ({ row }) => {
        // 可选从DictEnum中获取 DictEnum.CURRENCY 便于维护
        return renderDict(row.currency, 'currency');
      },
    },
  },
  {
    title: '首付比例',
    field: 'downpaymentPercent',
  },
  {
    title: '合同总价',
    field: 'totalAmount',
  },
  {
    title: '已付金额',
    field: 'paidAmount',
  },
  {
    title: '未付金额',
    field: 'unpayAmount',
  },
  {
    title: '销售数量',
    field: 'saleCount',
  },
  {
    title: '合同状态',
    field: 'status',
    slots: {
      default: ({ row }) => {
        return renderDict(row.status, 'approve_status');
      },
    },
  },
];

// 审核人列配置
const approverColumn = {
  title: '审核人',
  field: 'approverId',
};

// 根据状态动态生成列配置
export const getColumns = (status?: string): VxeGridProps['columns'] => {
  const columns = [...baseColumns];
  
  // 待提交和待审核状态不显示审核人列
  if (status !== 'draft' && status !== 'waiting') {
    columns.push(approverColumn);
  }
  
  return columns;
};

// 默认列配置（保持向后兼容）
export const columns: VxeGridProps['columns'] = getColumns();

